const { redis } = require('./connect')
/**
 * 用户打开二维码之后，设置 redis: { [ticket]: 'null' }
 * 扫码完成后：{ [ticket]: [openid] }
 * 返回 jwt，删除
 */

/** 申请了二维码后，1 小时内有效 */
function setTicket(ticket) {
  return redis.set(ticket, 'null', 'EX', 60 * 60)
}

function updateTicket(ticket, openid) {
  return redis.set(ticket, openid)
}

function getTicket(ticket) {
  return redis.get(ticket)
}

function delTicket(ticket) {
  return redis.del(ticket)
}

function setAccessToken(access_token, expire_seconds) {
  return redis.set('G_weixin_access_token', access_token, 'EX', expire_seconds)
}
function getAccessToken() {
  return redis.get('G_weixin_access_token')
}

module.exports = {
  Ticket: {
    set: setTicket,
    get: getTicket,
    update: updateTicket,
    remove: delTicket,
  },
  AccessToken: {
    set: setAccessToken,
    get: getAccessToken,
  }
}